﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using UniRlv.Utility;
using UniRlv.Utility.Extend;

namespace UniRlv.UI.Main
{
    public partial class FrmBase : OfficeForm
    {
        //声明 API 函数
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern IntPtr SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        public FrmBase()
        {
            InitializeComponent();

            this.TopLevel = false;
            this.Dock = DockStyle.Fill;
            this.FormBorderStyle = FormBorderStyle.None;
            this.EnableGlass = false;
            this.Visible = false;
        }

        private void FrmBase_Load(object sender, EventArgs e)
        {
            WorkSpace.GetInstance().CodeScanEvent += CodeScanEventPrc;
        }

        private void CodeScanEventPrc(object sender, CodeScanEventArg e)
        {
            Action<string, string> act = new Action<string, string>((s1, s2)=>
            {
                try
                {
                    OnScanCode(s1, s2);
                }
                catch (Exception)
                {

                }
                
            });

            if (!this.IsDisposed && this.IsHandleCreated)
            {
                try
                {
                    this.Invoke(act, e.codeOrg, e.code24bit);
                }
                catch (Exception)
                {
                }

            }

        }

        public virtual void OnScanCode(string orgCode, string validCode)
        {

        }

        public virtual bool IsInit()
        {
            return true;
        }

        protected void Wait()
        {
            this.Cursor = Cursors.WaitCursor;
        }

        protected void WaitBack()
        {
            this.Cursor = Cursors.Default;
        }

        protected ToolStripButton AddToolButton(string name, string caption)
        {
            toolStrip1.SuspendLayout();
            ToolStripButton btn = new ToolStripButton();

            btn.Image = global::UniRlv.UI.Main.Properties.Resources.node;
            btn.ImageTransparentColor = System.Drawing.Color.Magenta;
            btn.Name = name;
            btn.Size = new System.Drawing.Size(78+(caption.Length-2)*40, 43);
            btn.Text = caption;
            btn.Visible = true;

            toolStrip1.Items.Add(btn);
            toolStrip1.ResumeLayout(false);
            toolStrip1.PerformLayout();

            return btn;
        }

        #region 消息提示框
        public void ShowErrorMessageBox(Exception exp)
        {
            ShowErrorMessageBox("出现异常", exp.Message);
        }

        public void ShowErrorMessageBox(string caption, string info)
        {
            MessageBox.Show(this, info, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void ShowWarnningMessageBox(string caption, string info)
        {
            MessageBox.Show(this, info, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public void ShowInfoMessageBox(string caption, string info)
        {
            MessageBox.Show(this, info, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public DialogResult ShowConfirmDialog(string caption, string info)
        {
            return MessageBox.Show(this, info, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
        }
        #endregion

        private void FrmBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            WorkSpace.GetInstance().CodeScanEvent -= CodeScanEventPrc;
        }

        private void FrmBase_Activated(object sender, EventArgs e)
        {
            
        }

        private void FrmBase_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
    }
}
